using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public class NewsBll
{
    DBAccess db = new DBAccess();

    public NewsBll()
    {

    }
 
    public DataTable getAllNews()
    {
        return db.executeSelect("GetAllNews");
    }

    public News getNewsById(int newsId)
    {
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@NewsId", SqlDbType.Int);
        para[0].Value = newsId;
        DataTable dt = db.executeSelect("GetNewsById", para);
        News news = new News();
        news.NewsId = int.Parse(dt.Rows[0][0].ToString());
        news.Title = dt.Rows[0][1].ToString();
        news.Content = dt.Rows[0][2].ToString();
        news.CreateDate = DateTime.Parse(dt.Rows[0][3].ToString());        
        return news;
    }

    public bool insertNews(News news)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[3];        
        para[0] = new SqlParameter("@Title", SqlDbType.NVarChar);
        para[0].Value = news.Title;
        para[1] = new SqlParameter("@Content", SqlDbType.NText);
        para[1].Value = news.Content;
        para[2] = new SqlParameter("@CreateDate", SqlDbType.DateTime);
        para[2].Value = news.CreateDate;
        if (db.execute("InsertNews", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool updateNewsById(News news)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[3];
        para[0] = new SqlParameter("@NewsId", SqlDbType.Int);
        para[0].Value =  news.NewsId;
        para[1] = new SqlParameter("@Title", SqlDbType.NVarChar);
        para[1].Value =  news.Title;
        para[2] = new SqlParameter("@Content", SqlDbType.NText);
        para[2].Value =  news.Content;        
        if (db.execute("UpdateNewsById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }

    public bool deleteNewsById(int newsId)
    {
        bool result;
        SqlParameter[] para = new SqlParameter[1];
        para[0] = new SqlParameter("@NewsId", SqlDbType.Int);
        para[0].Value = newsId;
        if (db.execute("DeleteNewsById", para) > 0)
        {
            result = true;
        }
        else
        {
            result = false;
        }
        return result;
    }
}
